/*DONE* Table [dbo].[<CompanyName>$Vendor$5ecfc871-5d82-43f1-9c54-59685e82318d]  2/2  Script Date: 30.7.2020 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Vendor$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Vendor$5ecfc871-5d82-43f1-9c54-59685e82318d](
	[timestamp] [timestamp] NOT NULL,
	[No_] [nvarchar](20) NOT NULL,
	[RTC Filter Field] [int] NOT NULL,
	[Buyer Group Code] [nvarchar](10) NOT NULL,
	[Buyer ID] [nvarchar](50) NOT NULL,
	[ASN Trust Profile] [nvarchar](20) NOT NULL,
 CONSTRAINT ['+@Company+'$Vendor$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].['+@Company+'$Vendor$5ecfc871-5d82-43f1-9c54-59685e82318d] ADD  CONSTRAINT [MDF_D47DA64FE8CBC8225CEF5822FD07C0B0E3075EAF85FDA0F08D6111B9EE0FBE07]  DEFAULT (N'''') FOR [ASN Trust Profile]'

EXEC(@CreateTable)

END